<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="onClickReset">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="pyjm" class="w-[350px] mr-2">
          <a-input v-model:value="formState.pyjm" placeholder="姓名/助记码/卡号/手机号/证件号/押金单号" />
        </a-form-item>
        <a-form-item name="dateRange" label="收款日期" class="w-[320px]">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item label="状态" class="!w-64">
          <a-select v-model:value="formState.statuss" placeholder="请选择" mode="multiple" :max-tag-count="3" allow-clear>
            <a-select-option v-for="item in statusList" :key="item" :value="item.id">{{ item.title }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item name="brYwyName" label="业务员" class="!w-64">
          <SelectTable v-model:value="formState.brYwyName" type="员工" page-size="999" @select="onSelectEmployee('brYwy', $event)" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <a-button type="primary" class="mb-2" @click="onClickAdd()"> 新增 </a-button>
      <BaseExport type="门诊" name="押金管理" code="00001194" :search-params="formState" class="ml-2">导出</BaseExport>
      <BaseTable :loading="loading" column-code="00001194" fill :scroll="{ x: 2100 }" :data-source="tableData" bordered :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'status'">
            <a-tag v-if="record.status === '有效'" color="blue">待使用</a-tag>
            <a-tag v-else-if="record.status === '已使用'" color="green">已使用</a-tag>
            <a-tag v-else color="red">已退款</a-tag>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" danger size="small" :disabled="record.depositStatus === '2' || record.depositStatus === '1'" @click="onHandleReturn(record)">
              退还押金
            </a-button>
            <a-divider type="vertical" />
            <a-button type="link" size="small" @click="onPrint(record)"> 打印 </a-button>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :patient-info="patientInfo" @success="onSuceess" />
    <Refund
      v-model:open="refundOpen"
      :business-data="{ pjhm: rowData.pjhm, bcyt: rowData.dpJe, bcyt_st: rowData.dpJe, ...patientInfo }"
      :loading-state="loadingState"
      @success="onSuceessRefund"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import { useRoute } from 'vue-router'
import SearchContainer from '@/components/SearchContainer'
import { post } from '@/utils/request'
import Refund from '../components/RefundModal.vue'
import useStore from '../store'
import Edit from './Edit'

const store = useStore()
const route = useRoute()
const rowData = ref({})
const patientInfo = ref({})
const editOpen = ref(false)
const refundOpen = ref(false)
const loadingState = ref(false)
const formRef = ref(null)
const statusList = ref([
  { id: 0, title: '待使用' },
  { id: 1, title: '已使用' },
  { id: 2, title: '已退款' }
])
const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  settlementType: '1',
  statuss: []
})

const onSelectEmployee = (key, select) => {
  formState.value[key + 'Name'] = select.empName
  formState.value[key] = select.empId
}

const onClickAdd = () => {
  patientInfo.value = {}
  editOpen.value = true
}

const onClickReset = () => {
  formRef.value.resetFields()
  formState.value.statuss = []
  formState.value.brYwy = ''
  getData()
}

const onHandleReturn = async (row) => {
  rowData.value = row
  refundOpen.value = true
}
const { loading, tableData, pagination, getData } = useTable({
  url: '/outp/mzdj/getDepositSlipList',
  immediate: true,
  params: formState.value,
  formatParams: (params) => {
    return {
      ...params,
      stdate: params['dateRange']?.[0],
      eddate: params['dateRange']?.[1]
    }
  }
})
const onSuceess = async (row) => {
  editOpen.value = false
  getData()
  const { code, msg, data } = await post('/outp/outPrint/getDepositSlip', { lsh: row.lsh })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}
const onSuceessRefund = async (params) => {
  // params.lsh = params.lsh
  loadingState.value = true
  delete params.pjhm
  const { code, msg } = await post('/outp/mzdj/sendBackDepositSlip', {
    ysje: params.tkje,
    ssje: params.tkje,
    empId: rowData.value.empId,
    brId: rowData.value.brId,
    ...params,
    lsh: rowData.value.lsh
  })
  loadingState.value = false
  if (code !== 0) return message.error(msg)
  message.success(msg)
  refundOpen.value = false
  getData()
}
const onPrint = async (row) => {
  const { code, msg, data } = await post('/outp/outPrint/getDepositSlip', { lsh: row.lsh, type: 1 })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}
onMounted(() => {
  store.patientInfo = {}
})

watch(
  () => store.patientInfo,
  (val) => {
    if (val && val.brId) {
      if (route.query?.status) {
        patientInfo.value = { ...store.patientInfo }
        editOpen.value = true
      }
    }
  },
  { immediate: true }
)
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
